﻿using EquipmentManager.BOLogic;
using EquipmentManager.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EquipmentManager.Models;

namespace EquipmentManager.Controllers
{
    public class OperatorController : Controller
    {
        //
        // GET: /Operator/
        [Authorize]
        public ActionResult Index()
        {
            MainPageModel mainPageModel = new MainPageModel();
            UserBO usersBO = new UserBO();
            EquipmentBO equipmentBO = new EquipmentBO();

            mainPageModel.Equipments = equipmentBO.GetAllRecentOperEquipments();

            return View(mainPageModel);
        }

        [HttpGet]
        [Authorize]
        public ActionResult ProfileChange()
        {
            UserBO userBO = new UserBO();

            CreateModel<User> userModel = new CreateModel<User>();
            userModel.Instance = userBO.GetCurrentUser();
            userModel.Instance.Password = String.Empty;
            userModel.ParentId = userModel.Instance.Region.Id;

            return View(userModel);
        }

        [HttpPost]
        [Authorize]
        public ActionResult ProfileChange(CreateModel<User> userModel)
        {
            UserBO userBO = new UserBO();
            RegionBO regionBO = new RegionBO();

            userModel.Instance.Region = regionBO.GetRegion(userModel.ParentId);

            if (ModelState.IsValid)
            {
                if (userBO.EditUserProfile(userModel))
                {
                    return RedirectToAction("Index", "Operator");
                }

                return View(userModel);
            }
            else
            {
                ModelState.AddModelError("", "Правильно заполните поля!");
                return View(userModel);
            }
        }

        public ActionResult ExportAllEquipments()
        {
            EquipmentBO equipmqntBO = new EquipmentBO();

            return View("~/Views/Shared/ExportAllEquipments.cshtml", equipmqntBO.GetOperEquipments());
        }

        [Authorize]
        public ActionResult PostOffices()
        {
            PostOfficeBO postOfficeBO = new PostOfficeBO();
            RegionBO regionBO = new RegionBO();

            ListModel<PostOffice, Region> model = new ListModel<PostOffice, Region>();
            model.Parent = regionBO.GetOperRegion();
            model.ListItems = postOfficeBO.GetOperPostOffices();

            return View(model);
        }

        [Authorize]
        public ActionResult AllEquipments()
        {
            EquipmentBO equipmentBO = new EquipmentBO();
            SearchModel searchModel = new SearchModel();

            return View(searchModel);
        }

        [Authorize]
        public ActionResult SearchEquipmentResult(string searchText)
        {
            UserBO userBO = new UserBO();
            User curUser = userBO.GetCurrentUser();

            EquipmentBO equipmentBO = new EquipmentBO();

            List<EquipmentBase> equipmentModel = new List<EquipmentBase>();
            equipmentModel = equipmentBO.SearchEquipmentsOper(searchText);

            return View("_SearchEquipmentResultOper", equipmentModel);
        }

        [Authorize]
        public ActionResult Equipments(long PostOfficeId)
        {
            PostOfficeBO postOfficeBO = new PostOfficeBO();
            EquipmentBO equipmentBO = new EquipmentBO();

            ListModel<EquipmentBase, PostOffice> equipmentModel = new ListModel<EquipmentBase, PostOffice>();
            equipmentModel.Parent = postOfficeBO.GetPostOffice(PostOfficeId);
            equipmentModel.ListItems = equipmentBO.GetEquipments(PostOfficeId);

            return View(equipmentModel);
        }

        [Authorize]
        public ActionResult DetailEquipment(long EquipmentId)
        {
            EquipmentBO equipmentBO = new EquipmentBO();

            CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
            equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
            equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

            return View(equipmentModel);
        }

        [Authorize]
        [HttpGet]
        public ActionResult CreateEquipment(long PostOfficeId)
        {
            PostOfficeBO postOfficeBO = new PostOfficeBO();

            CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
            equipmentModel.ParentId = PostOfficeId;
            equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(PostOfficeId);
            equipmentModel.Instance.State = State.Good;

            return View(equipmentModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateEquipment(CreateModel<EquipmentBase> equipmentModel)
        {
            int checkNumb;
            EquipmentBO equipmentBO = new EquipmentBO();
            PostOfficeBO postOfficeBO = new PostOfficeBO();

            equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(equipmentModel.ParentId);

            if (ModelState.IsValid && (int.TryParse(equipmentModel.Instance.Number, out checkNumb) || equipmentModel.Instance.Number == "б/н"))
            {
                if (equipmentBO.CreateNewEquipment(equipmentModel))
                {
                    //используем ВьюБег для возврата к предыдущей странице
                    return RedirectToAction("Equipments", "Operator", new { PostOfficeId = equipmentModel.ParentId });
                }
                else
                {
                    ModelState.AddModelError("", "Правильно заполните поля!");
                    return View(equipmentModel);
                }
            }
            else
            {
                ModelState.AddModelError("", "Правильно заполните поля!");
                return View(equipmentModel);
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult EditEquipment(long EquipmentId)
        {
            EquipmentBO equipmentBO = new EquipmentBO();

            CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
            equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
            equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

            return View(equipmentModel);
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditEquipment(CreateModel<EquipmentBase> equipmentModel)
        {
            EquipmentBO equipmentBO = new EquipmentBO();
            PostOfficeBO postOfficeBO = new PostOfficeBO();

            equipmentModel.Instance.PostOffice = postOfficeBO.GetPostOffice(equipmentModel.ParentId);

            if (ModelState.IsValid)
            {
                if (equipmentBO.EditEquipment(equipmentModel))
                {
                    return RedirectToAction("Equipments", "Operator", new { PostOfficeId = equipmentModel.ParentId });
                }

                return View(equipmentModel);
            }
            else
            {
                ModelState.AddModelError("", "Правильно заполните поля!");
                return View(equipmentModel);
            }
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteEquipment(long EquipmentId)
        {
            EquipmentBO equipmentBO = new EquipmentBO();

            CreateModel<EquipmentBase> equipmentModel = new CreateModel<EquipmentBase>();
            equipmentModel.Instance = equipmentBO.GetEquipment(EquipmentId);
            equipmentModel.ParentId = equipmentModel.Instance.PostOffice.Id;

            return View(equipmentModel);
        }

        [HttpPost, ActionName("DeleteEquipment")]
        [Authorize]
        public ActionResult DeleteEquipmentConfirmed(CreateModel<EquipmentBase> equipmentModel, long EquipmentId)
        {
            EquipmentBO equipmentBO = new EquipmentBO();

            if (equipmentBO.DeleteEquipment(EquipmentId))
            {
                return RedirectToAction("Equipments", "Operator", new { PostOfficeId = equipmentModel.ParentId });
            }

            return View(EquipmentId);
        }

    }
}
